home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 289_01.zip / MOVE_GEN.C < prev    next >
Text File  |  1993-04-26  |  3KB  |  81 lines

  1. /*-----------------------------------------------------------------------------
  2. Move generator
  3.  
  4. Revision History
  5. ----------------
  6. Gary Culp   14 Dec 1988    Initial version.
  7. -----------------------------------------------------------------------------*/
  8. #include "othello.h"
  9.  
  10. /*
  11. Given a board, a starting cell in that board, the color of the player whose
  12. turn it is, and an array of ints for listing affected pieces,
  13. find a move & build the list of pieces affected by that move.
  14. The first entry in the list of affected pieces is the new piece.
  15.  
  16. Pieces are listed in the affected_list by the linear offset
  17. of the piece within the .board array.
  18.  
  19. Returns:
  20.    number of affected pieces (0 if no move was found)
  21.  
  22.    affected_list will contain the list of affected pieces.  This function
  23.    also uses affected_list as a workspace, so even "unused" elements of
  24.    affected_list will be changed upon return.
  25. */
  26. int
  27. find_move(
  28.    struct board_struct *board_ptr,
  29.    int start_displacement,
  30.    unsigned char movers_color,
  31.    int *affected_list)
  32. {
  33.    unsigned char *cell_ptr;
  34.    register unsigned char *p;
  35.    register int *aff_ptr;
  36.    int *aff_hold;
  37.    unsigned char hostile_color;
  38.    unsigned char *stop;
  39.    int axis_num;
  40.    int delta;
  41.    int pass;
  42.  
  43.    hostile_color = (US_PIECE | THEM_PIECE) ^ movers_color;
  44.    stop = &board_ptr->board[8][9];
  45.    aff_ptr = affected_list + 1;
  46.  
  47.    /* piece loop */
  48.    for (cell_ptr = &board_ptr->board[0][0] + start_displacement; ; cell_ptr++)
  49.    {
  50.       /* find an empty cell */
  51.       while (!BELONGS(*cell_ptr, NO_PIECE)) {
  52.          if (++cell_ptr >= stop) {
  53.             return (0);    /* no move was found */
  54.          }
  55.       }
  56.  
  57.       /* for each direction (i.e., + and - along each axis) */
  58.       for (axis_num = BD_NDX; axis_num <= V_NDX; axis_num++) {
  59.          delta = delta_array[axis_num];
  60.          for (pass = 0; pass < 2; pass++, delta = -delta) {
  61.             aff_hold = aff_ptr;  /* remember where we are in list */
  62.             p = cell_ptr;
  63.             while (BELONGS(*(p += delta), hostile_color)) {
  64.                *aff_ptr++ = p - &board_ptr->board[0][0];
  65.             }
  66.             if (!BELONGS(*p, movers_color)) {
  67.                /* Rip out the pieces we just put in the list;
  68.                   they wouldn't be captured by this move.
  69.                */
  70.                aff_ptr = aff_hold;
  71.             }
  72.          }
  73.       }
  74.  
  75.       if (aff_ptr - affected_list > 1) {
  76.          *affected_list = cell_ptr - &board_ptr->board[0][0];
  77.          return (aff_ptr - affected_list);
  78.       }
  79.    }
  80. }
  81.